*************
***Table 2***
*************

*Years since independence

clear all

import delimited "Datasets/iisystem.dat"
rename v1 gwno
rename v2 gwabb
rename v3 countryname
rename v4 sysmembfrom
rename v5 sysmembto

label variable gwno "Gleditsch & Ward country code"
label variable gwabb "Gleditsch & Ward country abbreviation"
label variable countryname "Gleditsch & Ward country name"
notes gwno: Source: Kristian S. Gleditsch & Michael D. Ward, 1999. A Revised List of Independent States since the Congress of Vienna. International Interactions 1999
notes gwabb: Source: Kristian S. Gleditsch & Michael D. Ward, 1999. A Revised List of Independent States since the Congress of Vienna. International Interactions 1999
notes countryname: Source: Kristian S. Gleditsch & Michael D. Ward, 1999. A Revised List of Independent States since the Congress of Vienna. International Interactions 1999

/* Including the Occupied Palestinian Territories from 1967 */

set obs `=_N+1'
replace countryname="Occupied Palestinian Territories" in l 
replace gwno=667 if countryname=="Occupied Palestinian Territories" 
notes gwno: The Occupied Palestinian Territories have been added to the original list as gwno= 667
replace gwabb="OPT" if countryname=="Occupied Palestinian Territories"
replace sysmembfrom="11:06:1967" if countryname=="Occupied Palestinian Territories"
replace sysmembto="31:12:2012" if countryname=="Occupied Palestinian Territories"

/* Creating stata date and numeric year variables */

capture drop smfromdate 
capture drop smtodate
capture drop smfromyear
capture drop smtoyear

gen smfromdate = .
gen smtodate = .
replace smfromdate = date(sysmembfrom,"DMY",2100)
replace smtodate = date(sysmembto,"DMY",2100)
format smfromdate %tdDDmonCCYY
format smtodate %tdDDmonCCYY
label variable smfromdate "GW system member from date"
label variable smtodate "GW system member to date"


gen smfromyear = .
gen smtoyear = .
replace smfromyear = year(smfromdate)
replace smtoyear = year(smtodate)
label variable smfromyear "GW system member from year"
label variable smtoyear "GW system member to year"

/* Adjustments to the Gleditsch & Ward country system dates */
/* Adjustments for states  existing in 1816 */
replace smfromyear = 1815 if smfromyear == 1816 

/* Independence dates */
capture drop indepyear
gen indepyear = .
replace indepyear = smfromyear if smfromyear != 1816
label variable indepyear "Year of independence, GW system"



/* Independence dates for coutnries with multiple entries */
/* Pre-1815 dates are from Strand et al. Why Waves (footnote 19) */
replace indepyear = 1804 if gwno == 41 
replace indepyear = 1878 if gwno == 340
replace indepyear = 1868 if gwno == 341
replace indepyear = 1918 if gwno == 366
replace indepyear = 1918 if gwno == 367
replace indepyear = 1918 if gwno == 368
replace indepyear = 1815 if gwno == 580 
replace indepyear = 1700 if gwno == 600 
replace indepyear = 1700 if gwno == 615 
replace indepyear = 1705 if gwno == 616 
replace indepyear = 1711 if gwno == 620 
replace indepyear = 1827 if gwno == 651
replace indepyear = 1747 if gwno == 700 
replace indepyear = 1700 if gwno == 775

/* The remaining pre-1815 countries: */
replace indepyear = 1700 if gwno == 200
replace indepyear = 1700 if gwno == 210
replace indepyear = 1700 if gwno == 220
replace indepyear = 1700 if gwno == 225
replace indepyear = 1700 if gwno == 230
replace indepyear = 1700 if gwno == 235
replace indepyear = 1700 if gwno == 300
replace indepyear = 1700 if gwno == 365
replace indepyear = 1700 if gwno == 380
replace indepyear = 1700 if gwno == 390
replace indepyear = 1700 if gwno == 698
replace indepyear = 1700 if gwno == 630
replace indepyear = 1700 if gwno == 640
replace indepyear = 1700 if gwno == 800
replace indepyear = 1700 if gwno == 710
replace indepyear = 1781 if gwno == 2
replace indepyear = 1811 if gwno == 150
replace indepyear = 1701 if gwno == 255
replace indepyear = 1720 if gwno == 325
replace indepyear = 1814 if gwno == 385
replace indepyear = 1768 if gwno == 790

*Independence

gen dur= smtoyear- smfromyear
sort gwno
by gwno: egen dur_sum=sum(dur)
by gwno: gen n=_n
*keep if n==1
drop if smtoyear < 1989
*by religion: egen rel_sum=sum(dur_sum)

*Independence since 1989
capture drop indep1 dur_indep1 dur_indep1_sum indep1byrel
gen indep1 = . 
sort gwno
replace smtoyear = 2014 if smtoyear == 2012
by gwno: replace indep1 = smfromyear if indep1 == .
by gwno: replace indep1 = 1989 if smfromyear < 1989

merge 1:1 gwno using "Datasets/PEWreligion_gwno.dta"

drop if _merge == 2
replace religion = 2 if countryname == "Yemen (Arab Republic of Yemen)"
replace religion = 6 if countryname == "Vietnam, Democratic Republic of"
replace religion = 1 if countryname == "German Democratic Republic"
replace religion = 1 if countryname == "Czechoslovakia"
replace religion = 1 if countryname == "Serbia"
replace religion = 2 if countryname == "Occupied Palestinian Territories"

capture drop if religion == .

gen dur_indep1 = smtoyear - indep1
sort gwno
by gwno: egen dur_indep1_sum=sum(dur_indep1)
sort religion
by religion: egen indep1byrel=sum(dur_indep1_sum)


******************************
*Years in intrastate conflict*
******************************

use "Datasets/124920_1ucdp-prio-_2015.dta", clear

drop if type == 1
drop if type == 2
drop if year < 1989
*keep if intensitylevel == 2

sort gwnoa year

drop if gwnoa == gwnoa[_n-1] & year==year[_n-1]

gen religion = .
replace religion = 7 if location == "China"
replace religion = 6 if inlist(location, "North Vietnam, South Vietnam", "South Vietnam", "Vietnam","Vietnam (North Vietnam)","South Vietnam, Vietnam (North Vietnam)")
replace religion = 5 if location == "Israel"
replace religion = 4 if inlist(location, "India","Nepal","Hyderabadh")
replace religion = 3 if inlist(location, "Laos","Thailand","Sri Lanka","Cambodia (Kampuchea)","Myanmar (Burma)")
replace religion = 2 if inlist(location, "Pakistan","Guinea-Bissau","Afghanistan","Tajikistan","Saudi Arabia","Sierra Leone","Indonesia","Syria","Uzbekistan")
replace religion = 2 if inlist(location, "Yemen", "South Yemen","Yemen (North Yemen)","Senegal","Lebanon","Iraq","Iran")
replace religion = 2 if inlist(location, "Bangladesh","Tunisia","Brunei","Guinea","Somalia","Gambia","Djibouti")
replace religion = 2 if inlist(location, "Azerbaijan","Burkina Faso","Morocco","Egypt","Malaysia","Comoros","Mauritania","Niger")
replace religion = 2 if inlist(location, "Mali", "Algeria", "Sudan", "Libya", "Turkey","Chad","Oman","Bosnia-Herzegovina")

replace religion = 1 if religion == .

gen conflict = .
replace conflict = 1 if type > 0

sort religion
by religion: egen war_by_rel=sum(conflict)

******************************
*Years in interstate conflict*
******************************

use "Datasets/124920_1ucdp-prio-_2015.dta", clear

keep if type == 2
drop if year < 1989
*keep if intensitylevel == 2
sort gwnoa year

drop if gwnoa == gwnoa[_n-1] & year==year[_n-1]

gen religion = .
replace religion = 7 if location == "China"
replace religion = 6 if inlist(location, "North Vietnam, South Vietnam", "South Vietnam", "Vietnam","Vietnam (North Vietnam)","South Vietnam, Vietnam (North Vietnam)")
replace religion = 5 if location == "Israel"
replace religion = 4 if inlist(location, "India","Nepal","Hyderabadh")
replace religion = 3 if inlist(location, "Laos","Thailand","Sri Lanka","Cambodia (Kampuchea)","Myanmar (Burma)")
replace religion = 2 if inlist(location, "Pakistan","Guinea-Bissau","Afghanistan","Tajikistan","Saudi Arabia","Sierra Leone","Indonesia","Syria","Uzbekistan")
replace religion = 2 if inlist(location, "Yemen", "South Yemen","Yemen (North Yemen)","Senegal","Lebanon","Iraq","Iran")
replace religion = 2 if inlist(location, "Bangladesh","Tunisia","Brunei","Guinea","Somalia","Gambia","Djibouti")
replace religion = 2 if inlist(location, "Azerbaijan","Burkina Faso","Morocco","Egypt","Malaysia","Comoros","Mauritania","Niger")
replace religion = 2 if inlist(location, "Mali", "Algeria", "Sudan", "Libya", "Turkey","Chad","Oman","Bosnia-Herzegovina")

replace religion = 1 if religion == .

gen conflict = .
replace conflict = 1 if type > 0

sort religion
by religion: egen war_by_rel=sum(conflict)

******************************
***Years in nonstate conflict**
*******************************

use "Datasets/124930_1ucdp_nonstate2015.dta", clear

split location, p(",")
split gwnolocation, p(",")


keep location1 year gwnolocation1

rename location1 location

save "Datasets/split1.dta", replace

use "Datasets/124930_1ucdp_nonstate2015.dta", clear

split location, p(",")

split gwnolocation, p(",")

keep location2 year gwnolocation2
replace location2 = trim(location2)
drop if location2 == ""

rename location2 location

save "Datasets/split2.dta", replace

use "Datasets/split1.dta"

merge m:m location year using "Datasets/split2.dta"

save "Datasets/split3.dta", replace


sort location year

drop if location == location[_n-1] & year==year[_n-1]

gen religion = .
replace religion = 7 if location == "China"
replace religion = 6 if inlist(location, "North Vietnam, South Vietnam", "South Vietnam", "Vietnam","Vietnam (North Vietnam)","South Vietnam, Vietnam (North Vietnam)")
replace religion = 5 if location == "Israel"
replace religion = 4 if inlist(location, "India","Nepal","Hyderabadh")
replace religion = 3 if inlist(location, "Laos","Thailand","Sri Lanka","Cambodia (Kampuchea)","Myanmar (Burma)")
replace religion = 2 if inlist(location, "Pakistan","Guinea-Bissau","Afghanistan","Tajikistan","Saudi Arabia","Sierra Leone","Indonesia","Syria","Uzbekistan")
replace religion = 2 if inlist(location, "Yemen", "South Yemen","Yemen (North Yemen)","Senegal","Lebanon","Iraq","Iran")
replace religion = 2 if inlist(location, "Bangladesh","Tunisia","Brunei","Guinea","Somalia","Gambia","Djibouti")
replace religion = 2 if inlist(location, "Azerbaijan","Burkina Faso","Morocco","Egypt","Malaysia","Comoros","Mauritania","Niger")
replace religion = 2 if inlist(location, "Mali", "Algeria", "Sudan", "Libya", "Turkey","Chad","Oman","Bosnia-Herzegovina","Kyrgyzstan")

replace religion = 1 if religion == .

gen conflict = .
replace conflict = 1 if year > 1988

sort religion
by religion: egen war_by_rel=sum(conflict)

******************************
***Years in one-sided conflict**
*******************************

use "Datasets/124932_1ucdp_onesided2015.dta", clear


split location, p(",")
split gwnolocation, p(",")

keep location1 year gwnolocation1

rename location1 location

save "Datasets/split4.dta", replace

use "Datasets/124932_1ucdp_onesided2015.dta", clear

split location, p(",")

split gwnolocation, p(",")

keep location2 year gwnolocation2
replace location2 = trim(location2)
drop if location2 == ""

rename location2 location

save "Datasets/split5.dta", replace


use "Datasets/124932_1ucdp_onesided2015.dta", clear

split location, p(",")

split gwnolocation, p(",")

keep location3 year gwnolocation3
replace location3 = trim(location3)
drop if location3 == ""

rename location3 location

save "Datasets/split6.dta", replace

use "Datasets/split4.dta", clear
merge m:m location year using "Datasets/split5.dta"
capture drop _merge
merge m:m location year using "Datasets/split6.dta"

sort location year

drop if location == location[_n-1] & year==year[_n-1]

gen religion = .
replace religion = 7 if location == "China"
replace religion = 6 if inlist(location, "North Vietnam, South Vietnam", "South Vietnam", "Vietnam","Vietnam (North Vietnam)","South Vietnam, Vietnam (North Vietnam)")
replace religion = 5 if location == "Israel"
replace religion = 4 if inlist(location, "India","Nepal","Hyderabadh")
replace religion = 3 if inlist(location, "Laos","Thailand","Sri Lanka","Cambodia (Kampuchea)","Myanmar (Burma)")
replace religion = 2 if inlist(location, "Pakistan","Guinea-Bissau","Afghanistan","Tajikistan","Saudi Arabia","Sierra Leone","Indonesia","Syria","Uzbekistan")
replace religion = 2 if inlist(location, "Yemen", "South Yemen","Yemen (North Yemen)","Senegal","Lebanon","Iraq","Iran")
replace religion = 2 if inlist(location, "Bangladesh","Tunisia","Brunei","Guinea","Somalia","Gambia","Djibouti")
replace religion = 2 if inlist(location, "Azerbaijan","Burkina Faso","Morocco","Egypt","Malaysia","Comoros","Mauritania","Niger")
replace religion = 2 if inlist(location, "Mali", "Algeria", "Sudan", "Libya", "Turkey","Chad","Oman","Bosnia-Herzegovina")
replace religion = 2 if inlist(location, "Kyrgyzstan","Jordan","Kuwait")

replace religion = 1 if religion == .

gen conflict = .
replace conflict = 1 if year > 1988

sort religion
by religion: egen war_by_rel=sum(conflict)

***************
***PST-score***
***************

use "Datasets/PTS2015.dta", clear

drop if Amnesty == . 
drop if Year < 1989
by COWnum, sort: egen avg_amn= mean(Amnesty) if Amnesty !=.
by COWnum, sort: egen avg_hrw = mean(HRW) if HRW !=.


rename COWnum gwno
rename Year year
replace gwno = 680 if gwno == 679
replace gwno = 817 if gwno == 816
replace gwno = 260 if gwno == 255
drop if gwno == 666
replace gwno = 666 if gwno == 666.001

merge m:1 gwno using "Datasets/PEWreligion_gwno.dta"
drop if religion == .
drop if Country == .

by religion, sort: egen rel_amn = mean(avg_amn)
by religion, sort: egen rel_hrw = mean(avg_hrw)
